package com.sdtele.tta.utils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

import com.sdtele.tta.dac.Dac;
import com.sdtele.tta.models.Task;
import com.sdtele.tta.models.Team;
import com.sdtele.tta.models.User;

public class TeamUtils {
	public static Map<String, Object> TeamToMap(Team team) {
		Map<String, Object> teamToMap = new HashMap<String, Object>();
		teamToMap.put("id", team.getId());
		teamToMap.put("name", team.getTeamName());
		teamToMap.put("upTeamId", team.getUpTeamId());
		teamToMap.put("cityName", team.getCityName());
		teamToMap.put("cityID", team.getCityID());
		return teamToMap;
	}
	
	
	public static Map<String, Object> TaskToMap(Task task) {
		Map<String, Object> taskToMap = new HashMap<String, Object>();
		taskToMap.put("id", task.getId());
		taskToMap.put("title", task.getTitle());
		taskToMap.put("ownerId", task.getOwnerId());
		taskToMap.put("description", task.getDescription());
		taskToMap.put("createTime", task.getCreateTime());
		taskToMap.put("deadLine", task.getDeadLine());
		taskToMap.put("emergency", task.getEmergency());
		taskToMap.put("fatherTaskId", task.getFatherTaskId());
		taskToMap.put("completion", task.getCompletion());
		taskToMap.put("state", task.getState());
		taskToMap.put("tags", task.getTags());
		taskToMap.put("updateTime", task.getUpdateTime());
		return taskToMap;
	}

	public static void fillSubteams(Map<String, Object> teamMap) {
		long teamId = (Long) teamMap.get("id");
		List<Team> teamList = Dac.getInstance().getsubTeams(teamId);
		List<Map<String, Object>> subTeamArray = new ArrayList<Map<String, Object>>();
		if (teamList != null)
			for (Team t : teamList) {
				Map<String, Object> tMap = TeamToMap(t);
				subTeamArray.add(tMap);
				fillSubteams(tMap);
			}
		teamMap.put("subTeams", subTeamArray);
	}

	public static void findRootTeamId(long teamId, Map<String, Long> teamMap) {
		long upTeamId = Dac.getInstance().getTeam(teamId).getUpTeamId();
		if (upTeamId != 0)
			findRootTeamId(upTeamId, teamMap);
		else
			teamMap.put("teamId", teamId);
	}

	public static void fillContextBook(Map<String, Object> teamMap) {
		long teamId = (Long) teamMap.get("id");
		//System.out.println(teamId);
		List<Team> teamList = Dac.getInstance().getsubTeams(teamId);
		List<Map<String, Object>> subTeamArray = new ArrayList<Map<String, Object>>();
		List<User> userList = Dac.getInstance().getUserbyIsroot(teamId);
		//System.out.println("userList=" + userList);
		if (teamList != null)
			for (Team t : teamList) {
				Map<String, Object> tMap = TeamToMap(t);
				fillContextBook(tMap);
				subTeamArray.add(tMap);
			}
		teamMap.put("subTeams", subTeamArray);
		teamMap.put("users", userList);
	}

	public static void teamAuthority(long managerTeamId, Set<Long> teamIdSet) {
		List<Team> teamList = Dac.getInstance().getsubTeams(managerTeamId);
		if (teamList != null)
			for (Team t : teamList) {
				teamIdSet.add(t.getId());
				teamAuthority(t.getId(), teamIdSet);
			}
	}

	public static void teamAuthorityByTeamName(long managerTeamId, Set<String> teamNameSet) {
		List<Team> teamList = Dac.getInstance().getsubTeams(managerTeamId);
		if (teamList != null)
			for (Team t : teamList) {
				teamNameSet.add(t.getTeamName());
				teamAuthorityByTeamName(t.getId(), teamNameSet);
			}
	}

	public static Map<String, Object> subUserMap(User user) {
		Map<String, Object> subUserMap = new HashMap<String, Object>();
		subUserMap.put("userId", user.getId());
		subUserMap.put("account", user.getAccount());
		subUserMap.put("name", user.getName());
		subUserMap.put("teamId", user.getTeamId());
		subUserMap.put("teamName", Dac.getInstance().getTeam(user.getTeamId()).getTeamName());
		subUserMap.put("phone", user.getPhone());
		subUserMap.put("email", user.getEmail());
		subUserMap.put("sex", user.getSex());
		subUserMap.put("avatar", user.getAvatar());
		subUserMap.put("isManager", user.getIsManager());
		return subUserMap;
	}

	public static void getSubUsers(Team team, List<Map<String, Object>> subUsers) {
		List<Team> teamList = Dac.getInstance().getsubTeams(team.getId());
		List<User> userList = Dac.getInstance().getUserbyteamId(team.getId());
		if (userList != null)
			for (User u : userList) {
				Map<String, Object> uMap = subUserMap(u);
				subUsers.add(uMap);
			}
		if (teamList != null)
			for (Team t : teamList)
				getSubUsers(t, subUsers);
	}

}
